<?php


namespace app\api\model\School;


use think\facade\Db;
use think\Model;

class SchoolWalletModel extends Model
{

    /**
     * 数据表名
     * @var string
     */
    protected $name = "school_wallet";

    /**
     * 根据条件查询单挑数据
     * @param $where
     * @param string $filed
     * @return array|Model|null
     * @throws
     */
    public static function getSchoolWalletWhere($where,$filed = "*")
    {
        $mer = Db::name("school_wallet")->field($filed)->where($where)->find();
        return $mer;
    }

    /**
     * 添加返回ID
     * @param $data
     * @return int
     */
    public static function addSchoolWallet($data)
    {
        $id = Db::name('school_wallet')->insertGetId($data);
        return $id;
    }


    /**
     * 入账
     * @param $school_id
     * @param $amount
     * @param $bill_use_type
     * @param $remark
     * @return bool
     * @throws
     */
    public static function walletIn($school_id, $amount, $bill_use_type ,$remark)
    {
        $flag = Db::name('school_wallet')->where([['school_id','=',$school_id]])->inc('amount', $amount)->update();
        if (!$flag) {
            return false;
        }
        $data = [
            'school_id' => $school_id,
            'bill_type' => 1,
            'amount' => $amount,
            'remark' => $remark,
            'bill_use_type' => $bill_use_type,
            'add_time' => date("Y-m-d H:i:s",time())
        ];
        $result = Db::name('school_wallet_bill')->insertGetId($data);
        if (!$result) {
            return false;
        }
        return true;
    }


    /**
     * 出账
     * @param $school_id
     * @param $amount
     * @param $bill_use_type
     * @param string $remark
     * @return bool
     * @throws
     */
    public static function walletOut($school_id, $amount, $bill_use_type ,$remark = '')
    {
        $flag = Db::name('school_wallet')->where([['school_id','=',$school_id]])->dec('amount', $amount)->update();
        if (!$flag) {
            return false;
        }
        $data = [
            'school_id' => $school_id,
            'bill_type' => 5,
            'amount' => $amount,
            'remark' => $remark,
            'bill_use_type' => $bill_use_type,
            'add_time' => date("Y-m-d H:i:s",time()),
        ];
        $result = Db::name('school_wallet_bill')->insertGetId($data);
        if (!$result) {
            return false;
        }
        return true;
    }

}